# !/cn/dbswitcher/mysql2mongo
# encode=utf-8
# author:crowhyc
# filename=KnowledgeBaseConverter.py

import datetime

from cn.dbswitcher.DatabaseSource import *
from cn.dbswitcher.entity.Content import *
from cn.dbswitcher.entity.ContentType import *


def getConn():
    # connect('cms', host='192.168.1.202', port=27017)
    conn = getMysqlConn("local")
    return conn


def executeSql(sql):
    conn = getConn()
    cur = conn.cursor()
    cur.execute(sql)
    return cur


def contenttransform():
    now = datetime.datetime.now()
    sql = Content().knhySql()
    cur = executeSql(sql)
    for ret in cur:
        Content().knhyContent(ret).save()
    print("怀孕知识库 总共导入数据 ", cur.rowcount, "行,总共使用时间 ", datetime.datetime.now() - now)
    now = datetime.datetime.now()
    cur = executeSql(Content().noteatSql())
    for ret in cur:
        Content().notEatContent(ret).save()
    print("能不能吃 总共导入数据 ", cur.rowcount, "行,总共使用时间 ", datetime.datetime.now() - now)

    # now = datetime.datetime.now() 暂时未使用
    # cur = executeSql(Content().indexrecommend())
    # for ret in cur:
    #     Content().indexContent(ret).save()
    # print("首页提醒 总共导入数据 ", cur.rowcount, "行,总共使用时间 ", datetime.datetime.now() - now)
    # now = datetime.datetime.now()
    cur = executeSql(Content().normalKn())
    for ret in cur:
        content = Content().normalContent(ret)
        content.save()
    print("热门知识 总共导入数据 ", cur.rowcount, "行,总共使用时间 ", datetime.datetime.now() - now)


def refreshContentTypeContents():
    types = ContentType.objects.all()
    count = 0
    for type in types:
        data = Content.objects(typeId=type.cid)
        contentsList = []
        for ret in data:
            contentsList.append(ret.cid)
        print(type.name + "导入内容" + str(contentsList.__len__()) + "条")
        type.contents = contentsList
        count = count + contentsList.__len__()
        type.save()
    print("总共导入内容数量为" + str(count) + "条")


def refreshOtherMethod():
    contents = Content.objects.all()
    invlidData = []
    for content in contents:
        type = ContentType.objects(cid=content.typeId)
        type = type.first()
        if (type == None):
            invlidData.append(content.oldId)
            continue
        contentList = type.contents
        if (contentList == None):
            contentList = [content.cid]
        else:
            contentList.append(content.cid)
        type.contents = contentList
        type.save()
    for d in invlidData:
        print("\"" + d + "\",")


def convertType():
    datas = ContentType.objects.all()
    for ret in datas:
        type = ContentType(cid=ret['cid'], _class=ret['_class'], name=ret['name'], position=ret['position'],
                           createDate=ret['createDate'], fId=ret['fId'], fName=ret['fName'], pic=ret['pic'],
                           extendPropAndVal=ret['extendPropAndVal'], isDeleted=ret['isDeleted'],
                           isSystemType=ret['isSystemType'], floor=ret['floor'], weight=ret['weight'], )
        type.switch_db("202")
        type.save()


def convertRemenType():
    now = datetime.datetime.now()
    datas = executeSql(ContentType().getRemenTypeSql())
    for ret in datas:
        ContentType().proRemenType(ret).save()
    print("热门知识类型 总共导入数据 ", datas.rowcount, "行,总共使用时间 ", datetime.datetime.now() - now)


def test():
    datas = list(ContentType.objects.all())
    count = 0
    for data in datas:
        count = count + len(data.contents)
    print(count)


def getChildrenContentsCount(fid):
    contentType = ContentType.objects(fId=fid)
    count = []
    for type in contentType:
        count = count + getChildrenContentsCount(type.cid)
        count = count + len(type.contents)
    return count


def tempBeiYunImport(database):
    result = executeSql(Content().tempBeiyunSql())
    for ret in result:
        Content().tempBeiyunContent(ret, database).save()


if __name__ == "__main__":
    # convertRemenType()
    contenttransform()
    # convertType()
    # refreshContentTypeContents()
    # refreshOtherMethod()
    # tempBeiYunImport('222')
